<?php
/**
 * @author Eni
 * @version 0.1.1
 */
class Model_User extends Pidguin_Db_Table_Abstract
{
	/**
	 * Table name
	 *
	 * @var string
	 */
	protected $_name = 'user';
	
	protected $_dependentTables = array('Model_UserGroup', 'Model_Page', 'Model_News', 'Model_UserMessages');
	
	/**
	 * @var int
	 */
	protected $_guestId = null;
	
	/**
	 * Instance of class
	 *
	 * @var Model_User
	 */
	private static $_instance = null;
	
	/**
	 * Get instance of class
	 *
	 * @return Model_User
	 */
	public static function getInstance()
	{
		if (self::$_instance === null) {
			self::$_instance = new self();
		}
		return self::$_instance;
	}
	
	/**
	 * Get guest row
	 * 
	 * @return Zend_Db_Table_Row|null
	 */
	public function getGuestRow()
	{
		return $this->findUserById($this->getGuestId());
	}
	
	/**
	 * Get ID guest
	 *
	 * @return int
	 */
	public function getGuestId()
	{
		if ($this->_guestId) {
			return $this->_guestId;
		}
		$row = $this->fetchRow($this->select()->from('user', array('id'))->where('login = ?', 'guest'));
		
		if (!$row) {
			throw new Zend_Db_Table_Exception('User guest not found');
		}
		$this->_guestId = $row->id;
		return $this->_guestId;
	}
	
	/**
	 * Get user row
	 *
	 * @param int $id
	 * @return Zend_Db_Table_Row|null
	 */
	public function findUserById($id)
	{
		$row = $this->fetchRow($this->select()->where('id = ?', $this->getAdapter()->quote(intval($id))));
		
		if ($row) {
			return $row;
		}
		
		return null;
	}
}